<?php
declare(strict_types=1);

namespace App\Services\Tools\AdapaySdk;

use JsonException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

class AdapayService
{
    private $payment;

    private $kernel;

    public function __construct(array $params, $id)
    {
        $config = new Config();
        $config->appId = $params['appId'];
        $config->apiKeyLive = $params['apiKeyLive'];
        $config->apiKeyTest = $params['apiKeyTest'];
        $config->rsaPriateKey = $params['rsaPriateKey'];
        $config->apiPublicKey = $params['apiPublicKey'];
        $config->notifyUrl = $params['notifyUrl'];
        $this->kernel = new Kernel($config);
        $this->payment = new Payment($this->kernel);
    }

    /**
     * @param string $orderNo
     * @param string $payAmt
     * @param string $title
     * @param string $desc
     * @param string $deviceIp
     * @return Dto\PaymentDto
     * @throws ClientExceptionInterface
     * @throws Exception\PaymentRequestExctption
     * @throws JsonException
     * @throws RedirectionExceptionInterface
     * @throws ServerExceptionInterface
     * @throws TransportExceptionInterface
     */
    public function aliPay(string $orderNo, string $payAmt, string $title, string $desc, string $deviceIp): Dto\PaymentDto
    {
        return $this->payment->aliPay(
            $orderNo,
            $payAmt,
            $title,
            $desc,
            $deviceIp
        );
    }

    public function verifySign(string $data, string $sign): bool
    {
        return $this->kernel->verifySign($data, $sign);
    }
}